package com.ruoyi.mdkt.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.mdkt.entity.Task;
import com.ruoyi.mdkt.entity.TaskInfo;
import com.ruoyi.mdkt.entity.TaskReceive;
import com.ruoyi.mdkt.entity.vo.SignVO;
import com.ruoyi.mdkt.service.ScoreLogService;
import com.ruoyi.mdkt.service.TaskInfoService;
import com.ruoyi.mdkt.service.TaskReceiveService;
import com.ruoyi.mdkt.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
@CrossOrigin

@RestController
@RequestMapping("/mdkt/sign")
public class SignController extends BaseController {

    public final static String SIGNPREFIX = "sign_task_";
    public final static String SIGNKEY = "sign_key_";

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private TaskReceiveService taskReceiveService;
    @Autowired
    private ScoreLogService scoreLogService;

    /**
     * 获取签到详情
     *
     * @param courseId
     * @return 签到信息
     */
    @GetMapping("/getSignInfo/{courseId}")
    public R getSignInfo(@PathVariable("courseId") String courseId) {
        String key = SIGNPREFIX + courseId;
        Long taskInfoID = redisCache.<Long>getCacheObject(key);
        if (taskInfoID == null || taskReceiveService.isSigned(getUserId(), taskInfoID) == true) {
            return R.fail("签到已结束或已签到");
        } else {
            TaskInfo taskInfo = taskInfoService.getById(taskInfoID);
            return R.ok(taskInfo);
        }
    }

    /**
     * 签到
     *
     * @param signVO
     * @return 签到是否成功
     */
    @PostMapping("/signIn")
    public R signIn(@RequestBody SignVO signVO) {
        String signKey = SIGNKEY + String.valueOf(signVO.getCourseId());
        String key = redisCache.<String>getCacheObject(signKey);
        Long taskInfoId = redisCache.<Long>getCacheObject(SIGNPREFIX + String.valueOf(signVO.getCourseId()));
        Task task = taskService.getSignTaskByCourseId(String.valueOf(signVO.getCourseId()));
        if (key.equals(signVO.getKey())) {
            if (taskReceiveService.isSigned(getUserId(), taskInfoId)) {
                return R.fail("不可重复签到");
            } else {
                TaskReceive taskReceive = new TaskReceive();
                taskReceive.setAnswer(key);
                taskReceive.setTaskId(task.getId());
                taskReceive.setTaskInfoId(taskInfoId);
                taskReceive.setScore(1);
                taskReceive.setType(4);
                taskReceive.setQuestion("签到码");
                taskReceive.setStudentUserId(getUserId());
                //保存到TaskReceive
                taskReceiveService.save(taskReceive);
                //记录ScoreLog
                scoreLogService.saveScoreLog(1, "签到加分", getUserId(), signVO.getCourseId());
                return R.ok();
            }
        } else {
            return R.fail("签到码错误");
        }
    }

    /**
     * 设置签到
     *
     * @param signVO
     * @return 本次签到的tsakinfo
     */
    @PostMapping("/setSignInfo")
    public R setSignInfo(@RequestBody SignVO signVO) {

        Task task = taskService.getSignTaskByCourseId(String.valueOf(signVO.getCourseId()));
        String key = SIGNPREFIX + String.valueOf(signVO.getCourseId());
        String signKey = SIGNKEY + String.valueOf(signVO.getCourseId());
        TaskInfo signTaskInfo = taskInfoService.createSignTaskInfo(signVO, task, getUserId());
        if (signVO.getType() == 0) {
            redisCache.setCacheObject(key, signTaskInfo.getId(), signVO.getTime(), TimeUnit.MINUTES);
        } else {
            redisCache.setCacheObject(key, signTaskInfo.getId(), signVO.getTime(), TimeUnit.MINUTES);
            redisCache.setCacheObject(signKey, signVO.getKey(), signVO.getTime(), TimeUnit.MINUTES);
        }
        return R.ok();
    }
}

